package com.rzico.quartz.job;

import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductService;
import com.rzico.core.entity.SysJob;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.service.SysJobService;
import com.rzico.core.service.SysPluginService;
import com.rzico.market.entity.MarketProduct;
import com.rzico.market.service.MarketProductService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 网易严选商品信息更新
 */
public class WYYXApiProductTask implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    protected SysPluginService sysPluginService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    protected MarketProductService marketProductService;

    @Autowired
    private ProductService productService;



    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {
            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里写你要做的事情
            /**
             * 获取当前商户的信息
             */
            String mchId = "";
            Long enterpriseId = 0L;
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("name","葫芦派");
            List<Enterprise> enterpriseList= enterpriseService.selectList(param);
            if (0 < enterpriseList.size()) {
                mchId = enterpriseList.get(0).getMchId();
                enterpriseId = enterpriseList.get(0).getId();
            }
            /**
             * 第一步，搜索网易严选插件
             */
            SysPlugin sysplugin = sysPluginService.findByPlugin(mchId, "marketWYYXPlugin");
            /**
             * 第二步，将网易严选商品保存至数据库
             */
            marketProductService.saveWYYXProduct(sysplugin);
            /**
             * 第三步，已上架到商品库（wx_product）中的网易严选商品sku每天定时检查
             */
            Map<String, Object> productParams = new HashMap<String, Object>();
            productParams.put("sn", "wyyx-");
            List<Product> productList = productService.selectList(productParams);
            if (0 < productList.size()) {
                marketProductService.updateWYYXProduct(sysplugin, enterpriseId, mchId, productList);
            }
            /**
             * 第四步，定时更新网易严选商品的信息
             */
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sn", "wyyx-");
            List<MarketProduct> marketProductList = marketProductService.selectList(map);
            marketProductService.updateWYYXMarketProduct(sysplugin, marketProductList);


            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
        sysJob.setExecStatus(0);
        sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
        sysJobService.updateByPrimaryKeySelective(sysJob);
    }

    }

}
